import { GameEvent } from "../../GameEvent";
import { Handler } from "../../app_base/base/utils/Handler";
import { Link } from "../../app_base/module/structure/Link";
import { app } from "../../base";
import { G_Data } from "../DataCenter";

export type StringOrNumber = string | number;
export type BaseStruct = {
    /**
     * 关卡解锁列表
     */
    unlockLevels: StringOrNumber[],
    /**
     * 关卡通关列表
     */
    passLevels: StringOrNumber[],

    /**
     * 入口是否看广告
     */
    isEnterAd: boolean

    circle: number;
}

export class BaseLink<T extends BaseStruct> extends Link<T> {
    _initData: T;
    _data: T;
    _hashPool = {}
    private _curLvId: number = 0

    constructor(linkName: string, initData: T) {
        const copiedObj = JSON.parse(JSON.stringify(initData));
        super(linkName, copiedObj);
        this._initData = initData;
        this._data = copiedObj;
    }

    protected _restSelfData(): void {
        const copiedObj = JSON.parse(JSON.stringify(this._initData));
        this._data = copiedObj;
    }

    get isEnterAd(): boolean {
        return this._data.isEnterAd;
    }

    set isEnterAd(value: boolean) {
        this._data.isEnterAd = value;
        this.updated("isEnterAd", this._data.isEnterAd, this._data.isEnterAd);
    }

    /**
     * 关卡解锁列表
     */
    public get unlockLevels(): StringOrNumber[] {
        return this._data.unlockLevels.concat();
    }

    /**
     * 解锁关卡
     * @param level 目标关卡
     */
    public unlockLevel(level: StringOrNumber): void {
        if (!this.isUnlock(level)) {
            this._data.unlockLevels.push(level);
            this.updated("unlockLevels", level, null);
        }
    }

    public setUnlockLevels(levels: StringOrNumber[]) {
        this._data.unlockLevels = levels
        this.updated("unlockLevels", this._data.unlockLevels, this._data.unlockLevels)
    }

    /**
     * 解锁关卡列表
     */
    public get unlockLevelsForServer(): string {
        return this._data.unlockLevels.join(",");
    }

    /**
     * 关卡是否解锁
     * @param level 目标关卡 
     * @returns 
     */
    public isUnlock(level: StringOrNumber): boolean {
        return this._data.unlockLevels.includes(level);
    }

    /**
     * 通关关卡列表
     */
    public get passLevels(): StringOrNumber[] {
        return this._data.passLevels.concat();
    }

    /**
     * 关卡是否通关
     * @param level 目标关卡
     * @returns 
     */
    public isPass(level: StringOrNumber): boolean {
        return this._data.passLevels.includes(level);
    }

    /**
     * 保存通关关卡
     * @param level 目标关卡
     */
    public passLevel(level: StringOrNumber): void {
        if (!this.isPass(level)) {
            this._data.passLevels.push(level);
            this.updated("passLevels", level, null);
        }
    }

    /**
     * clear通关关卡
     */
    public clearPassLevel() {
        this._data.passLevels = []
        this.updated("passLevels", this._data.passLevels, this._data.passLevels)
    }

    /**
     * 设置通关数据
     */
    public setPassLevels(levels: StringOrNumber[]) {
        this._data.passLevels = levels
        this.updated("passLevels", this._data.passLevels, this._data.passLevels)
    }

    /**
     * 通关关卡列表
     */
    public get passLevelsForServer(): string {
        return this._data.passLevels.join(",");
    }

    /**
    * 获取第一个未通关关卡
    */
    getFirstUnPassLevel(configs) {
        // return configs[7]
        for (let i = 0; i < configs.length; i++) {
            if (this.isPass(configs[i].tag)) {
                continue;
            }
            return configs[i];
        }
        //全部通关返回第一关
        return configs[0]
    }

    /**
    * 获取下一个关卡配置 
    */
    getNextLevelInfo(configs, curLvInfo) {
        for (let index = 0; index < configs.length; index++) {
            if (configs[index].tag == curLvInfo.tag) {
                // 是最后一关,返回第一关
                if (index == configs.length - 1) {
                    return configs[0]
                }
                return configs[index + 1]
            }
        }
        return null
    }

    /**
    *获取当前关卡索引
    */
    getCurLevelIndex(configs, cfg): number {
        for (let index = 0; index < configs.length; index++) {
            if (configs[index].tag == cfg.tag) {
                return index
            }
        }
        return -1
    }

    get circle() {
        return this._data.circle || 0;
    }
    set circle(value) {
        this._data.circle = value;
        this.updated("circle", value, null);
    }
}
